package com.jxsa.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.HashMap;
import java.util.Map;

public class Result {

    /**
     * 错误处理
     * @param resultEnum
     * @return
     */
    public static String error(ResultEnum resultEnum) {
        Map<String, Object> map = comMapData(resultEnum, null);
        return JSON.toJSONString(map, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteMapNullValue);
    }

    public static String error(ResultEnum resultEnum, Object o) {
        Map<String, Object> map = comMapData(resultEnum, o);
        return JSON.toJSONString(map, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteMapNullValue);
    }

    /**
     * 正确的返回的数据
     * @param resultEnum
     * @param
     * @return
     */
    public static String success(ResultEnum resultEnum) {
        Map<String, Object> map = comMapData(resultEnum, null);
        return JSON.toJSONString(map, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteMapNullValue);
    }

    public static String success(ResultEnum resultEnum, Object o) {
        Map<String, Object> map = comMapData(resultEnum, o);
        return JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);
    }

    private static Map<String, Object> comMapData(ResultEnum resultEnum, Object obj) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("code", resultEnum.getCode());
        map.put("msg", resultEnum.getMsg());
        if (null != obj) {
            map.put("data", obj);
        }
        return map;
    }
}
